package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.repository.GoodsSummaryWithPrice
import com.zxy.supplier_system.server.service.DirectSaleClientService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.time.OffsetDateTime

@RequestMapping("directSaleClients")
@RestController
class DirectSaleClientController(
    private val directSaleClientService: DirectSaleClientService
) {

    data class QueryDirectSaleClientsRequest(
        val name: String?,
    )

    @HasAnyAuthority(Authority.经销_客户_查询)
    @GetMapping(params = ["page"])
    fun queryDirectSaleClients(pageable: Pageable, queryDirectSaleClientsRequest: QueryDirectSaleClientsRequest): Page<QueryDirectSaleClientsResponse> {
        return this.directSaleClientService.queryDirectSaleClients(pageable, queryDirectSaleClientsRequest)
    }

    data class QueryDirectSaleClientsResponse(
        val name: String,
        val id: Int,
        val amount:Int,
        val lastOrderSubmittedDateTime:OffsetDateTime?,
    )

    data class UpdateDirectSaleClientRequest(
        val name: String,
    )

    @HasAnyAuthority(Authority.经销_客户_修改)
    @PutMapping("{id}")
    fun updateDirectSaleClient(@PathVariable id: Int, @RequestBody updateDirectSaleClientRequest: UpdateDirectSaleClientRequest) {
        this.directSaleClientService.updateDirectSaleClient(id, updateDirectSaleClientRequest)
    }

    @HasAnyAuthority(Authority.经销_客户_新增)
    @PostMapping
    fun createDirectSaleClient(@RequestBody updateDirectSaleClientRequest: UpdateDirectSaleClientRequest) {
        this.directSaleClientService.createDirectSaleClient(updateDirectSaleClientRequest)
    }

    @HasAuthority(Authority.经销_客户_删除)
    @DeleteMapping("{id}")
    fun deleteDirectSaleClient(@PathVariable id: Int) {
        this.directSaleClientService.deleteDirectSaleClient(id)
    }

    @GetMapping
    fun getAllDirectSaleClients(): List<IntIdAndName> {
        return this.directSaleClientService.getAllDirectSaleClients()
    }

    @HasAnyAuthority(Authority.经销_客户_查询)
    @GetMapping("{id}")
    fun getDirectSaleClientDetail(@PathVariable id: Int):DirectSaleClientService.DirectSaleClientDetail{
        return this.directSaleClientService.getDirectSaleClientDetail(id)
    }

    @GetMapping(params = ["projection=summary"])
    @HasAnyAuthority(Authority.经销_客户_查询)
    fun queryDirectSaleClientSummary(keyword:String?,pageable: Pageable):Page<IntIdAndName>{
        return this.directSaleClientService.queryDirectSaleClientSummary(keyword,pageable)
    }

    /**
     * 查询所有商品包含零售价和最近一次经销卖出的价格
     */
    @GetMapping("{id}/goods", params = ["projection=withHistoryOrder"])
    fun findAllGoodsWithHistoryOrder(@PathVariable id: Int):List<GoodsSummaryWithPrice>{
        return this.directSaleClientService.findAllGoodsWithHistoryOrder(id)
    }

}